package com.runa.monitor.platinterface.netservice.equipment;

import com.runa.monitor.platinterface.base.async.FutureDataTask;
import com.runa.monitor.platinterface.netservice.connect.GeneralRequest;
import com.runa.monitor.platinterface.netservice.connect.IRequest;
import com.runa.monitor.platinterface.netservice.connect.commstatus.CommStatus;
import com.runa.protocol.dto.Dtu86RequestData;
import com.runa.protocol.dto.DtuCMRequestData;
import com.runa.protocol.dto.DtuHDRequestData;
import com.runa.protocol.dto.IRequestData;
import com.runa.protocol.enums.DtuModel;
import com.runa.protocol.enums.ModbusType;

import java.util.Objects;

/**
 * @author zhuj
 */
public class DtuPlcService extends PlcService {

    private DtuModel model;

    private String dtuID;

    DtuPlcService(Integer id, int slaveId, String dtuID, Integer limit, Integer timeout, ModbusType modbusType, DtuModel model, FutureDataTask task) {
        super(id, slaveId, limit, timeout, modbusType, task);
        this.dtuID = dtuID;
        this.model = model;
    }

    @Override
    public IRequest<IRequestData> createReq(IRequestData requestData) {
        IRequest<IRequestData> request = null;
        switch (model) {
            case Dtu86:
                request = new GeneralRequest(new Dtu86RequestData(dtuID, requestData), this);
                break;
            case CaimorePort:
                request = new GeneralRequest(new DtuCMRequestData(dtuID, requestData), this);
                break;
            case HongDian7710:
                request = new GeneralRequest(new DtuHDRequestData(dtuID, requestData), this);
                break;
            case URG780:
                request = new GeneralRequest(requestData, this);
                break;
            default:
        }
        return request;
    }

    @Override
    void timeoutOverLimit(CommStatus status) {
        // 非主动连接暂不实现
    }

    public String getDtuID() {
        return dtuID;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof DtuPlcService) {
            DtuPlcService service = (DtuPlcService) obj;
            return Objects.equals(service.getId(), getId())
                    && Objects.equals(service.getDtuID(), dtuID);
        }
        return false;
    }

}
